import 'package:flutter/material.dart';
import 'package:x3_charts_core/chart_painter/dot_line_chart_painter.dart';
import 'package:x3_charts_core/chart_tools/chart_tools.dart';
import 'package:x3_charts_core/model/dot_line_chart_model.dart';

///**
///* @author hrongyong
///* @date   2/24/21
///* @email hrongyong@gmail.com
///* Description: 圆点折线图(x轴0为第一个起点)
///*/
class DotLineChartWidget extends StatefulWidget {
  ///图表数组
  final List<DotLineChartModel> chartList;

  ///第一个折线的点的颜色
  final Color firstPointColor;

  ///第一个折线的点的描述的颜色
  final Color firstDescriptionColor;

  ///第一个折线的颜色
  final Color firstPolylineColor;

  ///第二个折线的点的颜色
  final Color secondPointColor;

  ///第二个折线的点的描述的颜色
  final Color secondDescriptionColor;

  ///第二个折线的颜色
  final Color secondPolylineColor;

  ///圆点是否为实心
  final bool dotIsSolid;

  ///Y轴最大值
  final double yAxisMax;

  ///x轴线颜色
  final Color xAxisColor;

  ///x轴线描述颜色
  final Color xAxisDescriptionColor;

  ///y轴线颜色
  final Color yAxisColor;

  ///y轴线描述颜色
  final Color yAxisDescriptionColor;

  ///背景颜色
  final Color backgroundColor;

  DotLineChartWidget({
    @required this.chartList,
    @required this.yAxisMax,
    this.firstPointColor = const Color(0xFF50C4F7),
    this.firstDescriptionColor = const Color(0xFF50C4F7),
    this.firstPolylineColor = const Color(0xFF50C4F7),
    this.secondPointColor = const Color.fromARGB(255, 200, 58, 104),
    this.secondDescriptionColor = const Color.fromARGB(255, 200, 58, 104),
    this.secondPolylineColor = const Color.fromARGB(255, 200, 58, 104),
    this.xAxisColor = Colors.black12,
    this.yAxisColor = Colors.black12,
    this.xAxisDescriptionColor = Colors.black,
    this.yAxisDescriptionColor = Colors.black,
    this.backgroundColor = Colors.white,
    this.dotIsSolid = false,
  });

  @override
  _DotLineChartWidgetState createState() => _DotLineChartWidgetState();
}

class _DotLineChartWidgetState extends State<DotLineChartWidget> with SingleTickerProviderStateMixin {
  AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(milliseconds: 1000),
      vsync: this,
    )..forward();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.maxFinite,
      height: double.maxFinite,
      child: LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints) {
          return CustomPaint(
            size: Size(constraints.maxWidth, constraints.maxHeight),
            painter: DotLineChartPainter(
              chartList: widget.chartList,
              yAxisMax: ChartTools.calculateTheMaximum(widget.yAxisMax).toInt(),
              repaint: _controller,
              firstPointColor: widget.firstPointColor,
              firstDescriptionColor: widget.firstDescriptionColor,
              firstPolylineColor: widget.firstPolylineColor,
              secondPointColor: widget.secondPointColor,
              secondDescriptionColor: widget.secondDescriptionColor,
              secondPolylineColor: widget.secondPolylineColor,
              xAxisColor: widget.xAxisColor,
              xAxisDescriptionColor: widget.xAxisDescriptionColor,
              yAxisColor: widget.yAxisColor,
              yAxisDescriptionColor: widget.yAxisDescriptionColor,
              backgroundColor: widget.backgroundColor,
              dotIsSolid: widget.dotIsSolid,
            ),
          );
        },
      ),
    );
  }
}
